import { mockRequestClient as defHttp } from '#/api/request';
import type {
  Resource,
  ResourceQueryParam,
  ResourcePageResult,
  CreateResourceParam,
  UpdateResourceParam,
  ResourceRegistrationResult,
  ResourceDiscoveryResult,
} from './types';

enum Api {
  // 资源注册
  Register = '/resource/registry/register',

  // 资源发现
  Discover = '/resource/registry/discover',

  // 资源查询
  Query = '/resource/registry/query',

  // 资源详情
  Detail = '/resource/registry/detail',

  // 资源更新
  Update = '/resource/registry/update',

  // 资源删除
  Delete = '/resource/registry/delete',

  // 资源状态变更
  StatusChange = '/resource/registry/status',

  // 资源版本管理
  Versions = '/resource/registry/versions',

  // 资源依赖关系
  Dependencies = '/resource/registry/dependencies',

  // 资源统计
  Statistics = '/resource/registry/statistics',
}

/**
 * 资源注册中心API
 */
export const ResourceRegistryApi = {
  /**
   * 注册资源
   */
  register: (params: CreateResourceParam) =>
    defHttp.post<ResourceRegistrationResult>({
      url: Api.Register,
      data: params,
    }),

  /**
   * 发现资源
   */
  discover: (params: ResourceQueryParam) =>
    defHttp.get<ResourceDiscoveryResult>({
      url: Api.Discover,
      params,
    }),

  /**
   * 查询资源
   */
  query: (params: ResourceQueryParam) =>
    defHttp.get<ResourcePageResult>({
      url: Api.Query,
      params,
    }),

  /**
   * 获取资源详情
   */
  getDetail: (id: string) =>
    defHttp.get<Resource>({
      url: `${Api.Detail}/${id}`,
    }),

  /**
   * 更新资源
   */
  update: (id: string, params: UpdateResourceParam) =>
    defHttp.put<Resource>({
      url: `${Api.Update}/${id}`,
      data: params,
    }),

  /**
   * 删除资源
   */
  delete: (id: string) =>
    defHttp.delete<void>({
      url: `${Api.Delete}/${id}`,
    }),

  /**
   * 变更资源状态
   */
  changeStatus: (id: string, status: string) =>
    defHttp.put<void>({
      url: `${Api.StatusChange}/${id}`,
      data: { status },
    }),

  /**
   * 获取资源版本历史
   */
  getVersions: (id: string) =>
    defHttp.get<Resource[]>({
      url: `${Api.Versions}/${id}`,
    }),

  /**
   * 获取资源依赖关系
   */
  getDependencies: (id: string) =>
    defHttp.get<{
      dependencies: Resource[];
      dependents: Resource[];
    }>({
      url: `${Api.Dependencies}/${id}`,
    }),

  /**
   * 获取资源统计信息
   */
  getStatistics: () =>
    defHttp.get<{
      total: number;
      byType: Record<string, number>;
      byCategory: Record<string, number>;
      byStatus: Record<string, number>;
      recentCreated: Resource[];
      recentUpdated: Resource[];
    }>({
      url: Api.Statistics,
    }),
};
